<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" class="showSearch">
      <el-form-item label="车主姓名" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入车主姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="车主电话" prop="customerPhone">
        <el-input v-model="queryParams.customerPhone" placeholder="请输入车主电话" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="display: flex;justify-content: space-between;">
      <div>
        <el-button icon="Plus" type="primary" plain @click="handleAdd"> 开维修单</el-button>
      </div>
      <div>
        <el-row>
          <el-button :icon="Refresh" size="large" circle />
        </el-row>
      </div>
    </div>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="tableRef"
      class="table">
      <el-table-column label="序号" align="center" type="index" width="60" />
      <el-table-column label="车主姓名" align="center" prop="customerName" width="180" />
      <el-table-column label="车主电话" align="center" prop="customerPhone" width="180" />
      <el-table-column label="车型" align="center" prop="vehicleModel" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="维修单状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.status == 1" type="success" size="large">已完成</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger" size="large">已作废</el-tag>
          <el-tag v-if="scope.row.checkStatus == 1" type="success" size="large">已完工</el-tag>
          <el-tag v-if="scope.row.payStatus == 1" type="success" size="large">已结算</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <div v-if="scope.row.status == 1 || scope.row.status == 2">
            <el-button type="primary">详情</el-button>
          </div>
          <div v-else>
            <el-button type="primary" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="primary" icon="View">预检</el-button>
            <el-button type="primary" icon="Tools">维修</el-button>
            <el-button v-if="scope.row.status != 2" type="danger" icon="Delete"
              @click="handleCancellationById(scope.row)">作废</el-button>
          </div>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 新增维修单对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="200px" class="custom-width">
        <!-- 没有对应字段 -->
        <!-- <el-form-item label="行驶证" prop="precheckName">
          <el-input v-model="form.precheckName" placeholder="请输入检查项名称" />
        </el-form-item> -->
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车架号(vin)" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入车架号(vin)" />
        </el-form-item>
        <el-form-item label="车主姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入车主姓名" />
        </el-form-item>
        <el-form-item label="车主电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入车主电话" />
        </el-form-item>
        <el-form-item label="车型" prop="vehicleModel">
          <el-input v-model="form.vehicleModel" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="接车公里数" prop="mileageNum">
          <el-input v-model="form.mileageNum" placeholder="请输入接车公里数" />
        </el-form-item>
        <!-- 没有给到字段 -->
        <!-- <el-form-item label="保险到期日期" prop="remark" >
          <el-date-picker v-model="form.insuranceExpirationDate" type="date" placeholder="请选择保险到期日期" :suffix-icon="Calendar"></el-date-picker>
        </el-form-item>
        <el-form-item label="年检到期日期" prop="remark">
          <el-date-picker v-model="form.insuranceExpirationDate" type="date" placeholder="请选择年检到期日期" :suffix-icon="Calendar"></el-date-picker>
        </el-form-item> -->
        <el-form-item label="燃料(能源类型)" prop="energyType">
          <el-input v-model="form.energyType" placeholder="请输入燃料(能源类型)" />
        </el-form-item>
        <el-form-item label="接车师傅" prop="pickCarUserName">
          <el-input style="width: 300px;" v-model="form.pickCarUserName" placeholder="请输入接车师傅" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Post">
import { listXctRepairShopOrder, cancellationById } from '@/api/repair/serve/workOrder'
import { toRaw, computed, ref } from 'vue'
import { ElButton, ElDrawer } from 'element-plus'
import { CircleCloseFilled } from '@element-plus/icons-vue'

import { ElMessageBox } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const formLabelWidth = '140px'

const data = reactive({
  form: { insuranceExpirationDate: '' },
  queryParams: {
    current: 1,
    pageSize: 10,
    customerName: '',
    customerPhone: '',
    licensePlate: '',
    vehicleModel: ''
  },
  rules: {
    customerName: [{ required: true, message: "车主姓名", trigger: "blur" }],
    customerPhone: [{ required: true, message: "车主电话", trigger: "blur" }],
    licensePlate: [{ required: true, message: "车牌号", trigger: "blur" }],
    vehicleModel: [{ required: true, message: "车型", trigger: "blur" }],
    mileageNum: [{ required: true, message: "接车公里数", trigger: "blur" }],
    vin: [{ required: true, message: "车架号(vin)", trigger: "blur" }],
    energyType: [{ required: true, message: "燃料类型", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  loading.value = true
  listXctRepairShopOrder(queryParams.value)
    .then((response) => {
      console.log(response)
      dataList.value = response.data.data
      total.value = response.data.total
      loading.value = false
    })
    .catch((err) => {
      loading.value = false
    })
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    customerName: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 修改按钮操作 */
function handleUpdate(row) {
  console.log(row)
  form.value = { ...row }
  open.value = true
  title.value = '修改检查项信息'
}

/** 提交按钮:等待新增和修改请求接口 */
function submitForm() {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        update(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改检查项成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
/** 作废按钮操作 cancellationById */
function handleCancellationById(row) {
  console.log(row)
  proxy.$modal
    .confirm('是否确认作废该维修单')
    .then(function () {
      return cancellationById(row.id)
    })
    .then((res) => {
      // console.log(res.data)
      if (res.data == true) {
        proxy.$modal.msgSuccess('作废成功')
      } else {
        proxy.$modal.msgError('作废失败')
      }
      getList()

    })
    .catch(() => { })
}

// 新增按钮操作
function handleAdd() {
  reset()
  open.value = true
  title.value = '开维修单'
}


getList();
</script>
<style lang="scss" scoped>
// .el-form.showSearch {
//   box-shadow: 1px 1px 10px 1px #d1cfcf;
//   margin-bottom: 20px;
//   height: 100px;
//   line-height: 100px;
//   overflow: hidden;
//   padding-left: 25px;
// }
:deep(.el-button) {
  margin-top: 10px;
  margin-bottom: 10px;
}

.el-table--default .el-table__cell {
  padding: 10px;
}

.custom-width .el-input {
  width: 300px;
}
</style>